package p7.code;

import java.util.Vector;

public class VectorStudy {

    public static Object getLast(Vector list) {
        int lastIndex = list.size()-1;
        return list.get(lastIndex);
    }

    public static void deleteLast(Vector list) {
        int lastIndex = list.size()-1;
        list.remove(lastIndex);
    }

    public static Object getLastSafe(Vector list){
        synchronized (list){
            int lastIndex = list.size()-1;
            return list.get(lastIndex);
        }
    }

    public static void deleteLastSafe(Vector list){
        synchronized (list){
            int lastIndex = list.size()-1;
            list.remove(lastIndex);
        }
    }


    public static void main(String[] args) {
        Vector vector = new Vector();
        for(int i=0;i<1000;i++){
            vector.add(i);
        }

        new Thread(()->{
//            for(int i=0;i<100;i++){
//                System.out.println(getLastSafe(vector));
//            }
            for(Object i:vector){
                System.out.println(i);
            }
        }).start();

        new Thread(()->{
            for(int i=0;i<100;i++){
                deleteLastSafe(vector);
            }
        }).start();


    }



}
